﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;

namespace MedicalDB
{
    public partial class AdaugareEvaluareSociala : UserControl
    {
        private Control redControl;
        string CNP;
        string limba;

        public AdaugareEvaluareSociala(string limba,string CNP)
        {
            InitializeComponent();
            Rename(limba);
            this.CNP = CNP;
            this.limba = limba;
        }

        private void btAdaugaEvaluareSociala_Click(object sender, EventArgs e)
        {

            if (redControl != null)
                changeColor(redControl);

            EvaluareSociala es = new EvaluareSociala();
            es.IdPacient = CNP;
            es.DataEvaluari = dtDataEvaluariiSociale.Value;
            es.RezultateSociale = tbRezultateSociale.Text;
            es.RezultateAbilitati = tbRezultateAbilitati.Text;
            es.ConcluziiAsistent = tbConcluziiAsistent.Text;

            List<Control> toValidate = new List<Control>();
            bool isValid = true;

            // Validare AdaugareEvaluareSociala
            toValidate.Add(dtDataEvaluariiSociale);
            toValidate.Add(tbRezultateSociale);
            toValidate.Add(tbRezultateAbilitati);
            toValidate.Add(tbConcluziiAsistent);

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            Dictionary<string, string> errors = xmlParser.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");


            foreach (Control tb in toValidate)
            {
                Validation val = new Validation(tb, tb.Text);
                isValid = val.validate();

                if (isValid == false)
                {
                    string eroare = val.getError();
                    redControl = tb;
                    MessageBox.Show(errors["CampIncorect"], "Error",
                       MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }

            //salvare in BD
            
            if (isValid == true)
            {
                try
                {
                    PersistenceManager pm = PersistenceManager.Instance;
                    pm.Save(es);
                    MessageBox.Show(errors["okBD"], "Information",
                           MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show(errors["ErrorBD"], "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }
        private void changeColor(Control cont)
        {
            if (cont.GetType() == typeof(TextBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(DateTimePicker))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(ComboBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
        }
    }
}
